package com.tianyu.inspection.common.component;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tianyu.inspection.common.HttpRequest;
import com.tianyu.inspection.entity.*;
import com.tianyu.inspection.mapper.*;
import io.ocs.model.OcsSimpleModel;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 校验 校验失败为true, 校验成功为false
 * @Date 2021/3/2 - 11:55
 **********************************/

@Component
public class ValidateModel implements Validate{

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    @Resource
    private FlyTaskMapper flyTaskMapper;

    @Resource
    private UploadFileMapper uploadFileMapper;

    @Resource
    private DangerProcessMapper dangerProcessMapper;

    @Resource
    private DangerMapper dangerMapper;

    @Resource
    private DangerTypeMapper dangerTypeMapper;

    @Resource
    private DangerLevelMapper dangerLevelMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private PileCardMapper pileCardMapper;

    @Resource
    private GeneralFacilitiesMapper generalFacilitiesMapper;

    @Resource
    private HighConsequenceAreasMapper highConsequenceAreasMapper;

    @Resource
    private ConstructionUnitsMapper constructionUnitsMapper;

    @Resource
    private CameraMapper cameraMapper;

    @Resource
    private ConstructionTypeMapper constructionTypeMapper;

    @Resource
    private CameraTypeMapper cameraTypeMapper;

    @Resource
    private HighConsequenceAreasTypeMapper highConsequenceAreasTypeMapper;

    @Resource
    private PileCardTypeMapper pileCardTypeMapper;

    @Resource
    private GeneralFacilitiesTypeMapper generalFacilitiesTypeMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private PipelineMapper pipelineMapper;

    @Resource
    private AppConstants appConstants;

    @Override
    public <T> boolean validateIdArray(BaseMapper<T> mapper, String type) {
        JSONArray jsonArray = JSONArray.parseArray(type);
        if (jsonArray.size() == 0) {
            return false;
        }
        return jsonArray.size() != mapper.selectCount(new QueryWrapper<T>().in("id", jsonArray));
    }

    /**
     * 上传图片校验[1.路径是否真实存在, 2.文件类别是否对应]
     *
     * @param category 文件类别
     * @param path     文件OSS路径
     * @return 校验不通过为true, 通过为false
     */
    @Override
    public boolean uploadImageValidate(int category, String path) {
        return path.length() == 0 || !ocsSimpleModel.isObjectExist(path);
    }


    @Override
    public boolean uploadTelemetryValidate(String path) {
        if (path.length() == 0 || !ocsSimpleModel.isObjectExist(path.substring(1))) {
            return true;
        }
        if (!path.contains("/telemetry/")) {
            if (0 == uploadFileMapper.selectCount(new QueryWrapper<UploadFile>().eq("url", path))) {
                ocsSimpleModel.deleteObject(path.substring(1));
            }
            return true;
        }
        return false;
    }

    /**
     * 上传客户端软件校验[1.路径是否真实存在, 2.文件类别是否对应]
     *
     * @param path 文件OSS路径
     * @return 校验不通过为true, 通过为false
     */
    @Override
    public boolean uploadClientValidate(String path) {
        if (!ocsSimpleModel.isObjectExist(path.substring(1))) {
            return true;
        }
        if (!path.contains("/client/") && (!path.contains("/client/")) && (!path.contains("/client/")) && !path.contains("/client/")) {
            if (0 == uploadFileMapper.selectCount(new QueryWrapper<UploadFile>().eq("url", path))) {
                ocsSimpleModel.deleteObject(path.substring(1));
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean flyTaskIdValidate(int id, int projectId) {
        return 0 == flyTaskMapper.selectCount(new QueryWrapper<FlyTask>()
                .eq("id", id).eq("project_id", projectId));
    }

    @Override
    public boolean dangerProcessIdValidate(int id, int projectId) {
        return 0 == dangerProcessMapper.selectCount(new QueryWrapper<DangerProcess>()
                .eq("id", id).eq("project_id", projectId));
    }

    @Override
    public boolean dangerIdValidate(int id, int projectId) {
        return 0 == dangerMapper.selectCount(new QueryWrapper<Danger>()
                .eq("id", id).eq("project_id", projectId));
    }

    @Override
    public boolean inspectorIdValidate(int id, int projectId) {
        return 0 == userMapper.selectCount(new QueryWrapper<User>()
                .eq("id", id).eq("project_id", projectId).eq("role_id", 4));
    }

    @Override
    public boolean userIdValidate(int id, int projectId) {
        return 0 == userMapper.selectCount(new QueryWrapper<User>()
                .eq("id", id).eq("project_id", projectId));
    }

    @Override
    public ConstructionUnits constructionUnitsNumberValidate(String number, int projectId) {
        return constructionUnitsMapper.selectOne(new QueryWrapper<ConstructionUnits>()
                .eq("number", number).eq("project_id", projectId));
    }

    @Override
    public Camera cameraNumberValidate(String number, int projectId) {
        return cameraMapper.selectOne(new QueryWrapper<Camera>()
                .eq("number", number).eq("project_id", projectId));
    }

    @Override
    public HighConsequenceAreas highConsequenceAreasNumberValidate(String number, int projectId) {
        return highConsequenceAreasMapper.selectOne(new QueryWrapper<HighConsequenceAreas>()
                .eq("number", number).eq("project_id", projectId));
    }

    @Override
    public PileCard pileCardNumberValidate(String number, int projectId) {
        return pileCardMapper.selectOne(new QueryWrapper<PileCard>()
                .eq("system_stake_number", number).eq("project_id", projectId));
    }

    @Override
    public GeneralFacilities generalFacilitiesNumberValidate(String number, int projectId) {
        return generalFacilitiesMapper.selectOne(new QueryWrapper<GeneralFacilities>()
                .eq("number", number).eq("project_id", projectId));
    }

    @Override
    public boolean generalFacilitiesIdValidate(int generalFacilitiesId) {
        return 0 == generalFacilitiesTypeMapper.selectCount(new QueryWrapper<GeneralFacilitiesType>()
                .eq("id", generalFacilitiesId));
    }

    @Override
    public boolean pileCardTypeIdValidate(int pileCardTypeId) {
        return 0 == pileCardTypeMapper.selectCount(new QueryWrapper<PileCardType>()
                .eq("id", pileCardTypeId));
    }

    @Override
    public boolean highConsequenceAreasTypeIdValidate(int highConsequenceAreasTypeId) {
        return 0 == highConsequenceAreasTypeMapper.selectCount(new QueryWrapper<HighConsequenceAreasType>()
                .eq("id", highConsequenceAreasTypeId));
    }

    @Override
    public boolean constructionTypeIdValidate(int constructionTypeId) {
        return 0 == constructionTypeMapper.selectCount(new QueryWrapper<ConstructionType>()
                .eq("id", constructionTypeId));
    }

    @Override
    public boolean cameraTypeIdValidate(int cameraTypeId) {
        return 0 == cameraTypeMapper.selectCount(new QueryWrapper<CameraType>()
                .eq("id", cameraTypeId));
    }

    @Override
    public boolean dangerTypeIdValidate(int dangerTypeId) {
        return 0 == dangerTypeMapper.selectCount(new QueryWrapper<DangerType>()
                .eq("id", dangerTypeId));
    }

    @Override
    public boolean dangerLevelIdValidate(int dangerTypeId) {
        return 0 == dangerLevelMapper.selectCount(new QueryWrapper<DangerLevel>()
                .eq("id", dangerTypeId));
    }

    /**
     * 项目ID是否存在
     * @param projectId
     * @return
     */
    @Override
    public boolean projectIdValidate(int projectId) {
        return 0 == projectMapper.selectCount(new QueryWrapper<Project>()
                .eq("id", projectId));
    }

    @Override
    public boolean pipelineIdValidate(int pipelineId, int projectId) {

        return 0 == pipelineMapper.selectCount(new QueryWrapper<Pipeline>()
                .eq("id", pipelineId).eq("project_id", projectId));
    }

    @Override
    public boolean pipelineStationIdValidate(int pipelineStationId, int projectId) {
        return 0 == generalFacilitiesMapper.selectCount(new QueryWrapper<GeneralFacilities>()
                .eq("id", pipelineStationId)
                .eq("general_facilities_type_id", 2)
                .eq("project_id", projectId));
    }

    /**
     * 御空登录校验
     * @param ykAccessKey 御空Key
     * @param ykAccessValue 御空Key对应密钥
     * @return ture 为校验失败
     */
    @Override
    public boolean validateYkAccount(String ykAccessKey, String ykAccessValue) {
        try {
            ModelMap login = HttpRequest.yuKongThirdLogin(appConstants.yuKongHttpHost, ykAccessKey, ykAccessValue);
            return Objects.isNull(login) || login.get("state").equals(0);
        } catch (Exception e) {
            return false;
        }
    }
}
